<%@ page language="java" contentType="text/html; charset=UTF-8" 
import="com.google.appengine.api.datastore.KeyFactory,org.windblog.core.entity.*,java.util.*,org.windblog.core.*"  pageEncoding="UTF-8"%>

<%
	//post.jsp用于对已存在的日志进行编辑。接受的HTTP GET参数如下
	//	action  : edit 编辑日志的标志。
	//  post    : post的keystring。用于查找要编辑的post。
	//  message : 1 "日志发布成功。"
	
	String action  = request.getParameter("action");
	String postkey = request.getParameter("post");
	String mnp     = request.getParameter("message");
	int messageNum = 0;
	Post post = null;
	if(postkey != null)
	 	post = PostService.getPost(postkey);
	if(post == null)
	{
		response.sendRedirect("/admin/newPost.jsp");
	}
	if(mnp!=null)
		messageNum = Integer.parseInt(mnp);
	String message = null;
    switch(messageNum){
		case 1  : message = "日志发表成功！<a href=\""+post.getPermalink()+"\">点此查看日志</a>";break;
		default : message = null;
	}
%>
		<jsp:include page="header.jsp"></jsp:include>
		<jsp:include page="sidebar.jsp"></jsp:include>
		<script type="text/javascript">
			$(function(){
				// Accordion
				$("#accordion").accordion("activate",0);
				$("#post_edit").prepend(">");
				$("#publish").button();
			});
		</script>
		
		<div id="content">
			<h2>Add New Post</h2>
			<hr/>
			<form action="setpost" method="post">
			
			<div id="publishpanel" class="ui-widget ui-widget-content ui-corner-all" style="float:right;margin-top:10px;padding:0;width:160px;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Publish</h3>
				<input id="publish" value="Publish" type="submit" />
			</div>
			
			<div id="categorypanel" class="ui-widget ui-widget-content ui-corner-all" style="clear:right;float:right;margin-top:10px;padding:0;width:160px;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Category</h3>
				<ul>
					<li><strong>All categories:</strong></li>
					<%
					List<Category> postcats = post.getCategories();
					List<Category> cats = CategoryService.getCategories();
					if(cats!=null)
					{
					for(Category cat :cats)
					{
					%>
					<li><input type="checkbox" name="categories" value="<%=cat.getName() %>" <%=postcats.contains(cat)?"checked=\"checked\"":"" %>/><%=cat.getName() %></li>
					<%
					}
					}
					%>
				</ul>
			</div>
			<% if(message!=null){%>
			<div class="ui-widget" style="width:50em">
			<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"> 
				<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
				<%= message %></p>
			</div>
			</div><br/>
			<%} 
			%>
			
			<input id="title" name="title" type="text" value="<%= post.getTitle() %>"/>
			<input type="hidden" name="edit" value="<%=KeyFactory.keyToString(post.getKey()) %>"/> 
			<textarea id="ed2" name="content" rows="15" cols="80" style="width: 80%">
				<%=post.getContent() %>
			</textarea>
			
			<div id="Excerptpanel" class="ui-widget ui-widget-content ui-corner-all" style="margin-top:10px;padding:0;width:70em;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Excerpt</h3>
				<input id="excerpt" name="excerpt"  type="text" value="<%= post.getExcerpt() %>"/>
			</div>
			
			<div id="discussionpanel" class="ui-widget ui-widget-content ui-corner-all" style="margin-top:10px;padding:0;width:70em;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Discussion</h3>
				<input id="allowComments" name="allowComments"  type="checkbox" <%= post.getCommentStatus().equals("ALLOW_COMMENTS")?"checked=\"checked\"":"" %>/>Allow comments on this post.<br/>
			</div>
			
			</form>
		</div>
		<jsp:include page="footer.jsp"></jsp:include>
